package com.dong.sqltoy.service;

import com.dong.sqltoy.model.User;
import com.sagframe.sagacity.sqltoy.plus.conditions.Wrappers;
import com.sagframe.sagacity.sqltoy.plus.conditions.query.LambdaQueryWrapper;
import com.sagframe.sagacity.sqltoy.plus.dao.SqlToyHelperDao;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.sagacity.sqltoy.dao.SqlToyLazyDao;
import org.sagacity.sqltoy.model.Page;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

@Slf4j
@Service
public class UserService
{

    @Resource
    private SqlToyHelperDao sqlToyDao;

    public Object saveUser(User user)
    {
        return sqlToyDao.save(user);
    }

    public User loadUser(User user)
    {
        return sqlToyDao.load(user);
    }

    public List<User> loadAllUsers(List<User> user)
    {
        return sqlToyDao.loadAll(user);
    }

    public Long update(User user)
    {
        return sqlToyDao.update(user);
    }

    public Long delete(User user)
    {
        return sqlToyDao.delete(user);
    }

    public List<User> page(String name)
    {
        LambdaQueryWrapper<User> wrapper = Wrappers.lambdaWrapper(User.class)
                .like(StringUtils.hasText(name), User::getName, name)
                .orderByDesc(User::getCreator);

        Page<User> page = sqlToyDao.findPage(wrapper, new Page<>(10, 1));

        log.info("分页查询完成: 总记录数={}, 当前页记录数={}", page.getRecordCount(), page.getRows() != null ? page.getRows().size() : 0);

        return page.getRows();
    }
}